/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ImageIcon;
import metodologia.Etapa;

/**
 *
 * @author Lindemberg
 */
public class JanelaDeDadosDasEtapasEditar extends javax.swing.JFrame {
    private Etapa etapa;
    private JanelaEtapas janela;
    private int indice;
    private ArrayList<String> titulos;
    /**
     * Creates new form JanelaDeDadosDasEtapas
     */
    public JanelaDeDadosDasEtapasEditar(Etapa etapa, JanelaEtapas aThis) {
        initComponents();
        this.setTitle("PWManager - Editar dados das Etapas");
        ImageIcon imagemTituloJanela = new ImageIcon(getClass().getResource("/ImageFiles/icon_PW.png"));  
        setIconImage(imagemTituloJanela.getImage());
        this.setLocationRelativeTo(null);
        this.etapa = etapa;
        habilitarPlanilha(false);
        janela = aThis;
        indice = 0;
    }
    
    public void setDados(String titulo, String descricao, boolean planilha, String nlinhas, String ncolunas, int indice){
        jtNome.setText(titulo);
        jtDescricao.setText(descricao);
        jcbPlanilha.setSelected(planilha);
        if(jcbPlanilha.isSelected()){
            jtLinha.setText(nlinhas);
            jtColuna.setText(ncolunas);
            habilitarPlanilha(true);
            titulos = etapa.getTitulos();
        }else{
            titulos = new ArrayList<>();
        }
        this.indice = indice;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jlNome = new javax.swing.JLabel();
        jlDescricao = new javax.swing.JLabel();
        jtNome = new javax.swing.JTextField();
        jtDescricao = new javax.swing.JTextField();
        jcbPlanilha = new javax.swing.JCheckBox();
        jlLinhas = new javax.swing.JLabel();
        jtLinha = new javax.swing.JTextField();
        jlColunas = new javax.swing.JLabel();
        jtColuna = new javax.swing.JTextField();
        jbOk = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jbTitulos = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Dados das Etapas");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados das Etapas"));

        jlNome.setText("Nome:");

        jlDescricao.setText("Descrição:");

        jcbPlanilha.setText("Tem planilha");
        jcbPlanilha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbPlanilhaActionPerformed(evt);
            }
        });

        jlLinhas.setText("Nº de linhas:");

        jlColunas.setText("Nº de colunas:");

        jbOk.setText("OK");
        jbOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbOkActionPerformed(evt);
            }
        });

        jbCancelar.setText("Cancelar");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jbTitulos.setText("Titulos");
        jbTitulos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbTitulosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlDescricao)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jcbPlanilha)
                        .addGap(18, 18, 18)
                        .addComponent(jlLinhas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtLinha, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlColunas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtColuna, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbTitulos)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(jbOk, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlNome)
                    .addComponent(jtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlDescricao)
                    .addComponent(jtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbPlanilha)
                    .addComponent(jlLinhas)
                    .addComponent(jtLinha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlColunas)
                    .addComponent(jtColuna, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbOk)
                    .addComponent(jbTitulos)
                    .addComponent(jbCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcbPlanilhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbPlanilhaActionPerformed
        if(jcbPlanilha.isSelected()){
            habilitarPlanilha(true);
        }else{
            habilitarPlanilha(false);
        }
    }//GEN-LAST:event_jcbPlanilhaActionPerformed

    private void jbOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbOkActionPerformed
        etapa.setTitulo(jtNome.getText());
        etapa.setDescricao(jtDescricao.getText());
        etapa.setPlanilha(jcbPlanilha.isSelected());
        if(jcbPlanilha.isSelected()){
            etapa.setnLinhas(jtLinha.getText());
            etapa.setnColunas(jtColuna.getText());
        }else{
            etapa.setnLinhas("0");
            etapa.setnColunas("0");
        }
        etapa.setTitulos(titulos);
        System.out.println("Vai entrar no laço agora");
        for(Iterator i = titulos.iterator(); i.hasNext();){
            String a = (String)i.next();
            System.out.println(a);
        }
        janela.setarEtapa(etapa, indice);
        this.dispose();
    }//GEN-LAST:event_jbOkActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void jbTitulosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbTitulosActionPerformed
        JanelaDeTitulos j = new JanelaDeTitulos(Integer.parseInt(jtColuna.getText()), this, etapa.getTitulos());
        j.setVisible(true);
    }//GEN-LAST:event_jbTitulosActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbOk;
    private javax.swing.JButton jbTitulos;
    private javax.swing.JCheckBox jcbPlanilha;
    private javax.swing.JLabel jlColunas;
    private javax.swing.JLabel jlDescricao;
    private javax.swing.JLabel jlLinhas;
    private javax.swing.JLabel jlNome;
    private javax.swing.JTextField jtColuna;
    private javax.swing.JTextField jtDescricao;
    private javax.swing.JTextField jtLinha;
    private javax.swing.JTextField jtNome;
    // End of variables declaration//GEN-END:variables

    private void habilitarPlanilha(boolean habilitar) {
        jlLinhas.setEnabled(habilitar);
        jlColunas.setEnabled(habilitar);
        jtLinha.setEditable(habilitar);
        jtColuna.setEditable(habilitar);
        jbTitulos.setEnabled(habilitar);
    }
    
    public void setTitulos(ArrayList<String> titulos) {
        this.titulos = titulos;
        etapa.setTitulos(titulos);
        System.out.println("Tamanho dos títulos: " + titulos.size() + " " + titulos.toString());
        janela.setTitulos(titulos);
        for(Iterator i = titulos.iterator(); i.hasNext();){
            String s = (String)i.next();
            System.out.println(s);
        }
    }
}
